package com.shuaishuai.ketaiheadlines.action;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shuaishuai.ketaiheadlines.biz.Newscategoriesbiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/newscategories")
public class Newscategoriesaction {

    @Autowired(required = false)
    private Newscategoriesbiz newscategoriesbiz;

    @RequestMapping("/selectListNewscategories")
    public PageInfo selectListNewscategories(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam String name){
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo(newscategoriesbiz.getListNewscategories(name));
    }

    @RequestMapping("/deleteNewscategory/{categoryId}")
    public Map<String,String> delNewscategories(@PathVariable Integer categoryId){
        Map<String,String> map=new HashMap<>();
        if (newscategoriesbiz.deleteNewscategories(categoryId)>0){
            map.put("code","200");
            map.put("msg","删除成功");
        }else {
            map.put("code","500");
            map.put("msg","删除失败");
        }
        return map;
    }

    @RequestMapping("/addNewscategory/{name}")
    public Map<String,String> addNewscategories(@PathVariable String name){
        Map<String,String> map=new HashMap<>();
        if(newscategoriesbiz.insertNewscategories(name)>0){
            map.put("code","200");
            map.put("msg","添加成功");
        }else {
            map.put("code","500");
            map.put("msg","添加失败");
        }
        return map;
    }

    @RequestMapping("/updateNewscategory/{categoryId}/{name}")
    public Map<String,String> updateNewscategories(@PathVariable Integer categoryId, @PathVariable String name){
        Map<String,String> map=new HashMap<>();
        if(newscategoriesbiz.updateNewscategories(categoryId,name)>0){
            map.put("code","200");
            map.put("msg","修改成功");
        }else {
            map.put("code","500");
            map.put("msg","修改失败");
        }
        return map;
    }
}
